package com.jay.compass;

import com.jay.compass.utils.VibratorUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

public class CalibrateActivity extends Activity implements SensorEventListener {
	private SensorManager mSensorManager;
	private Sensor mAccSensor;

	private float mx[] = { -8, -5, 0, 5, 8, 5, 0, -4 };
	private float my[] = { 0, -7, -8, -7, 0, 4, 7, 5 };
	private int mask[] = { 1, 1, 1, 1, 1, 1, 1, 1 };
	private boolean gone = false;

	private Button skipButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_calibrate);
		mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		mAccSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

		skipButton = (Button) findViewById(R.id.skip_bt);
		skipButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (!gone) {
					gone = true;
					VibratorUtil.Vibrate(CalibrateActivity.this, 1000);
					Intent intent = new Intent(CalibrateActivity.this,
							CompassActivity.class);
					startActivity(intent);

					finish();
				}
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (mAccSensor != null) {
			mSensorManager.registerListener(this, mAccSensor,
					SensorManager.SENSOR_DELAY_GAME);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (mAccSensor != null) {
			mSensorManager.unregisterListener(this);
		}
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		float x = event.values[SensorManager.DATA_X];
		float y = event.values[SensorManager.DATA_Y];
		// float z = event.values[SensorManager.DATA_Z];
		for (int i = 0; i < 8; i++) {
			if ((x > (mx[i] - 1.5f) && (x < (mx[i] + 1.5f)))
					&& ((y > (my[i] - 1.5f) && (y < (my[i] + 1.5f))))) {
				mask[i] = 0;
			}
		}

		int sum = 0;
		for (int i = 0; i < 8; i++) {
			sum += mask[i];
		}
		// System.out.println("" + sum);

		if (sum < 2) {
			if (!gone) {
				gone = true;
				System.out.println("Done!!!!!!!!!!!!!!!!!!!!");
				VibratorUtil.Vibrate(this, 1000);
				Intent intent = new Intent(CalibrateActivity.this,
						CompassActivity.class);
				startActivity(intent);

				finish();
			}
		}
	}

}
